package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/shirou/gopsutil/v3/cpu"
	"github.com/shirou/gopsutil/v3/disk"
	"github.com/shirou/gopsutil/v3/host"
	"github.com/shirou/gopsutil/v3/mem"
	"net"
	"net/http"
	"runtime"
	"starry_sky/internal/core"
	"starry_sky/internal/tools/logger"
)

type ServerMessage struct {
	Os                  string    `json:"os"`
	Platform            string    `json:"platform"`
	PlatformVersion     string    `json:"platformVersion"`
	Hostname            string    `json:"hostname"`
	CpuInfos            []CpuInfo `json:"cpuInfos"`
	CpuPercent          string    `json:"cpuPercent"`
	VmStatTotal         string    `json:"vmStatTotal"`
	VmStatAvailable     string    `json:"vmStatAvailable"`
	VmStatUsedPercent   string    `json:"vmStatUsedPercent"`
	DiskStatTotal       string    `json:"diskStatTotal"`
	DiskStatFree        string    `json:"diskStatFree"`
	DiskStatUsedPercent string    `json:"diskStatUsedPercent"`
	NetInfos            []NetInfo `json:"netInfos"`
	GoroutineNum        int       `json:"goroutineNum"`
}

type CpuInfo struct {
	ModelName string `json:"modelName"`
	Cores     int32  `json:"cores"`
}
type NetInfo struct {
	Name  string   `json:"name"`
	Addrs []string `json:"addrs"`
}

func GetServerMessage(c *gin.Context) {
	serverMessage := ServerMessage{}
	serverMessage.CpuInfos = make([]CpuInfo, 0)
	serverMessage.NetInfos = make([]NetInfo, 0)
	// 获取操作系统信息
	hostInfo, err := host.Info()
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.Err.WithMsg("获取操作系统信息失败"))
		return
	}
	// 操作系统
	serverMessage.Os = hostInfo.OS
	//平台
	serverMessage.Platform = hostInfo.Platform
	//平台版本
	serverMessage.PlatformVersion = hostInfo.PlatformVersion
	//主机名
	serverMessage.Hostname = hostInfo.Hostname

	// 获取CPU信息
	cpuInfos, err := cpu.Info()
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.Err.WithMsg("获取CPU信息失败"))
		return

	}
	for _, ci := range cpuInfos {
		serverMessage.CpuInfos = append(serverMessage.CpuInfos, CpuInfo{
			//cpu型号
			ModelName: ci.ModelName,
			//逻辑处理器数
			Cores: ci.Cores,
		})
	}
	cpuPercent, err := cpu.Percent(0, false)
	if logger.HasError(err) {
		c.JSON(http.StatusInternalServerError, core.Err.WithMsg("获取CPU使用率失败"))
		return
	}
	serverMessage.CpuPercent = fmt.Sprintf("%.2f%%", cpuPercent[0])

	// 获取内存信息
	vmStat, _ := mem.VirtualMemory()
	serverMessage.VmStatTotal = fmt.Sprintf("%.2f GB", float64(vmStat.Total)/1024/1024/1024)
	serverMessage.VmStatAvailable = fmt.Sprintf("%.2f GB", float64(vmStat.Available)/1024/1024/1024)
	serverMessage.VmStatUsedPercent = fmt.Sprintf("%.2f%%", vmStat.UsedPercent)

	// 获取磁盘信息
	diskStat, _ := disk.Usage("/")
	serverMessage.DiskStatTotal = fmt.Sprintf("%.2f GB", float64(diskStat.Total)/1024/1024/1024)
	serverMessage.DiskStatFree = fmt.Sprintf("%.2f GB", float64(diskStat.Free)/1024/1024/1024)
	serverMessage.DiskStatUsedPercent = fmt.Sprintf("%.2f%%", diskStat.UsedPercent)

	// 获取网络信息
	interfaces, _ := net.Interfaces()
	for _, inter := range interfaces {
		netInfo := NetInfo{
			Name:  inter.Name,
			Addrs: make([]string, 0),
		}
		addrs, err := inter.Addrs()
		if logger.HasError(err) {
			continue
		}
		for _, addr := range addrs {
			netInfo.Addrs = append(netInfo.Addrs, addr.String())
		}
		serverMessage.NetInfos = append(serverMessage.NetInfos, netInfo)
	}
	//当前活跃协程数
	serverMessage.GoroutineNum = runtime.NumGoroutine()
	c.JSON(http.StatusOK, core.OK.WithData(serverMessage))
}
